1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect;
18  
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  import com.google.common.annotations.GwtCompatible;
22  import com.google.common.base.Supplier;
23  
24  import java.util.Comparator;
25  import java.util.Map;
26  import java.util.Set;
27  import java.util.SortedMap;
28  import java.util.SortedSet;
29  
30  /**
31   * Implementation of {@code Table} whose iteration ordering across row keys is
32   * sorted by their natural ordering or by a supplied comparator. Note that
33   * iterations across the columns keys for a single row key may or may not be
34   * ordered, depending on the implementation. When rows and columns are both
35   * sorted, it's easier to use the {@link TreeBasedTable} subclass.
36   *
37   * <p>The {@link #rowKeySet} method returns a {@link SortedSet} and the {@link
38   * #rowMap} method returns a {@link SortedMap}, instead of the {@link Set} and
39   * {@link Map} specified by the {@link Table} interface.
40   *
41   * <p>Null keys and values are not supported.
42   *
43   * <p>See the {@link StandardTable} superclass for more information about the
44   * behavior of this class.
45   *
46   * @author Jared Levy
47   */
48  @GwtCompatible
49  class StandardRowSortedTable<R, C, V> extends StandardTable<R, C, V>
50      implements RowSortedTable<R, C, V> {
51    /*
52     * TODO(jlevy): Consider adding headTable, tailTable, and subTable methods,
53     * which return a Table view with rows keys in a given range. Create a
54     * RowSortedTable subinterface with the revised methods?
55     */
56  
57    StandardRowSortedTable(SortedMap<R, Map<C, V>> backingMap,
58        Supplier<? extends Map<C, V>> factory) {
59      super(backingMap, factory);
60    }
61  
62    private SortedMap<R, Map<C, V>> sortedBackingMap() {
63      return (SortedMap<R, Map<C, V>>) backingMap;
64    }
65  
66    /**
67     * {@inheritDoc}
68     *
69     * <p>This method returns a {@link SortedSet}, instead of the {@code Set}
70     * specified in the {@link Table} interface.
71     */
72    @Override public SortedSet<R> rowKeySet() {
73      return (SortedSet<R>) rowMap().keySet();
74    }
75  
76    /**
77     * {@inheritDoc}
78     *
79     * <p>This method returns a {@link SortedMap}, instead of the {@code Map}
80     * specified in the {@link Table} interface.
81     */
82    @Override public SortedMap<R, Map<C, V>> rowMap() {
83      return (SortedMap<R, Map<C, V>>) super.rowMap();
84    }
85  
86    @Override
87    SortedMap<R, Map<C, V>> createRowMap() {
88      return new RowSortedMap();
89    }
90  
91    private class RowSortedMap extends RowMap implements SortedMap<R, Map<C, V>> {
92      @Override
93      public SortedSet<R> keySet() {
94        return (SortedSet<R>) super.keySet();
95      }
96  
97      @Override
98      SortedSet<R> createKeySet() {
99        return new Maps.SortedKeySet<R, Map<C, V>>(this);
100     }
101 
102     @Override
103     public Comparator<? super R> comparator() {
104       return sortedBackingMap().comparator();
105     }
106 
107     @Override
108     public R firstKey() {
109       return sortedBackingMap().firstKey();
110     }
111 
112     @Override
113     public R lastKey() {
114       return sortedBackingMap().lastKey();
115     }
116 
117     @Override
118     public SortedMap<R, Map<C, V>> headMap(R toKey) {
119       checkNotNull(toKey);
120       return new StandardRowSortedTable<R, C, V>(
121           sortedBackingMap().headMap(toKey), factory).rowMap();
122     }
123 
124     @Override
125     public SortedMap<R, Map<C, V>> subMap(R fromKey, R toKey) {
126       checkNotNull(fromKey);
127       checkNotNull(toKey);
128       return new StandardRowSortedTable<R, C, V>(
129           sortedBackingMap().subMap(fromKey, toKey), factory).rowMap();
130     }
131 
132     @Override
133     public SortedMap<R, Map<C, V>> tailMap(R fromKey) {
134       checkNotNull(fromKey);
135       return new StandardRowSortedTable<R, C, V>(
136           sortedBackingMap().tailMap(fromKey), factory).rowMap();
137     }
138   }
139 
140   private static final long serialVersionUID = 0;
141 }